clear all 

global Subsamples all period1 period2 period3 sectorA sectorB small large 

foreach sub_sample in $Subsamples {
use $DTA/Datos_ENIA_95_07_Processed.dta, clear
keep if `sub_sample' == 1 & totmuj <= 18 // Using values arguably not affected by the regulation.

****************************************
* 1.- Salarios:
****************************************

* For this part we will consider all the costs related to wages. In the outcomes we used a less comprehensive list but we were interested in the ratio, as opposed to the comparison with K as in this case.
local Descleg dlegprop dlegdirect dlegtesp dlegadmin dlegcoms dlegnocald dlegnocali dlegmant dlegserv dleaux
egen Dlegtot = rowtotal(`Descleg')
local Aporpatr aporcoms aporadmin apordirect aporprop apoaux aporserv aportesp aponocald
egen Aporpatr = rowtotal(`Aporpatr')

egen Totwages = rowtotal(rempag Dlegtot Aporpatr)
replace Totwages = Totwages

* Cálculo por trabajador:
scalar GAP = 0.25
gen W_avg = Totwages*inf/((tothom + totmuj))
gen Wm = W_avg*(tothom + totmuj)/(tothom+(1-GAP)*totmuj)
gen Ww = (1-GAP)*Wm
sum W*, separator(3)
/*
Table 11: Data CASEN

    Variable |        Obs        Mean    Std. dev.       Min        Max
-------------+---------------------------------------------------------
    wages_M1 |         51    5835.827    243.2917   5406.171    6321.68   		All_men
    wages_M2 |         51    5038.958    126.7288   4787.783    5313.99	  		period1_men
    wages_M3 |         51    6690.553    442.6554   5926.534   7576.235			period2_men
    wages_M4 |         51    6206.843    508.5093   5457.548   7581.236			period3_men
    wages_M5 |         51    5865.916    334.1381   5440.753    6589.74			SectorA_men
-------------+---------------------------------------------------------
    wages_M6 |         51    5790.541    199.8498   5354.722   6206.367			SectorB_men
    wages_M7 |         51    5329.704    236.4975   4905.833   5783.207			small_men 
    wages_M8 |         51    6138.062    241.9606   5685.401   6589.786			large_men
    wages_W1 |         51    4779.204    189.3716   4391.543   5095.232			All_women
    wages_W2 |         51    3375.572    271.0606   2923.828   3902.236	  		period1_women
-------------+---------------------------------------------------------
    wages_W3 |         51     7477.74    1515.327   5126.472   9887.738			period2_women
    wages_W4 |         51    5425.125    806.2391   4211.834   7334.689			period3_women
    wages_W5 |         51     6350.15    498.8398   5309.492    7205.48			SectorA_women
    wages_W6 |         51    3489.943    438.5602   2912.123   4232.363			SectorB_women
    wages_W7 |         51    4724.829    302.2533   4155.846   5225.678			small_women
-------------+---------------------------------------------------------
    wages_W8 |         51    4325.767    430.5799   3621.099   4994.572			large_women



replace Wm = 5836 if "`sub_sample'" == "all"
replace Ww = 4779 if "`sub_sample'" == "all"

replace Wm = 5039 if "`sub_sample'" == "period1"
replace Ww = 3376 if "`sub_sample'" == "period1"

replace Wm = 6691 if "`sub_sample'" == "period2"
replace Ww = 7478 if "`sub_sample'" == "period2"

replace Wm = 6207 if "`sub_sample'" == "period3"
replace Ww = 5425 if "`sub_sample'" == "period3"

replace Wm = 5330 if "`sub_sample'" == "small"
replace Ww = 4725 if "`sub_sample'" == "small"

replace Wm = 6138 if "`sub_sample'" == "large"
replace Ww = 4326 if "`sub_sample'" == "large"

replace Wm = 5866 if "`sub_sample'" == "sectorA"
replace Ww = 6350 if "`sub_sample'" == "sectorA"

replace Wm = 5791 if "`sub_sample'" == "sectorB"
replace Ww = 3490 if "`sub_sample'" == "sectorB"

*/

replace Wm = 5835.827 if "`sub_sample'" == "all"
replace Ww = 4779.204 if "`sub_sample'" == "all"

replace Wm = 5038.958 if "`sub_sample'" == "period1"
replace Ww = 3375.572 if "`sub_sample'" == "period1"

replace Wm = 6690.553 if "`sub_sample'" == "period2"
replace Ww = 7477.74 if "`sub_sample'" == "period2"

replace Wm = 6206.843  if "`sub_sample'" == "period3"
replace Ww = 5425.125 if "`sub_sample'" == "period3"

replace Wm = 5329.704 if "`sub_sample'" == "small"
replace Ww = 4724.829 if "`sub_sample'" == "small"

replace Wm = 6138.062 if "`sub_sample'" == "large"
replace Ww = 4325.767 if "`sub_sample'" == "large"

replace Wm = 5865.916 if "`sub_sample'" == "sectorA"
replace Ww = 6350.15 if "`sub_sample'" == "sectorA"

replace Wm = 5790.541 if "`sub_sample'" == "sectorB"
replace Ww = 3489.943 if "`sub_sample'" == "sectorB"

****************************************
* 2.- Retorno del capital:
****************************************

sum va
scalar va_M = r(mean)

replace rempag = rempag
sum rempag
scalar rempag_M = r(mean)

sum vstk
scalar vstk_M = r(mean)

scalar mpk = (va_M-rempag_M)/vstk_M

gen mpk = mpk

* Caselli y Feyrer:
gen R = 0.26

****************************************
* 3.- Theta y Rho:
****************************************

gen M_W = tothom/totmuj
gen K_W = vstk*inf/totmuj
gen K_M = vstk*inf/tothom
gen W = totmuj
gen M = tothom
gen K = vstk*inf

foreach var of varlist M_W K_W K_M Ww Wm W M K {
sum `var'
replace `var' = r(mean)
}

gen double theta = (-ln(Ww/R)+ln(K/W))/ln(K/W)
format %30.0g theta

* Ecuaciones antiguas:
* Ecuacion W/M:
*gen rho = 1/(ln(R^(theta/(theta-1))+Ww^(theta/(theta-1)))+ln(Ww^(-1/(theta-1))*M_W^(-1))*theta)*theta*(ln(R^(theta/(theta-1))+Ww^(theta/(theta-1)))-ln(Wm)+ln(Ww^(-1/(theta-1))*M_W^(-1)))		  
* Ecuación 
*gen rho_alt = -theta*(ln(Wm)-ln(K_M*(R+R^(-1/(theta-1))*Ww^(theta/(theta-1)))))/(ln(R^(theta/(theta-1))+Ww^(theta/(theta-1)))+theta*ln(R^(-1/(theta-1))*K_M))

* Ecuaciones nuevas:
gen double X = R^(theta/(theta-1))+Ww^(theta/(theta-1)) 
format %30.0g X
*Usando ecuación de M/W:
gen double rho = theta*(ln(M/W)-theta*ln(M/W)+ln(Wm)-theta*ln(Wm)-ln(Ww)-ln(X)+theta*ln(X))/(theta*ln(M/W)-(theta^2)*ln(M/W)-theta*ln(Ww)-ln(X)+theta*ln(X))
format %30.0g rho
*Usando ecuación de K/M:
gen double rho_alt = theta*((-1+theta)*ln(K/M)-ln(R)-(1-theta)*ln(X)+(1-theta)*ln(Wm))/(-theta*(1-theta)*ln(K/M)-theta*ln(R)-(1-theta)*ln(X))
format %30.0g rho_alt


****************************************
* 3.- Sigma:
****************************************

gen sigma     = ln(va_M)/ln(((K^theta+W^theta)^(rho/theta)+M^rho)^(1/rho))
gen sigma_alt = ln(va_M)/ln(((K^theta+W^theta)^(rho_alt/theta)+M^rho)^(1/rho_alt))

tempfile Parametros_`sub_sample'
keep theta rho rho_alt sigma sigma_alt Ww Wm mpk X M W K
keep if _n == 1
gen group = "`sub_sample'"
save `Parametros_`sub_sample''

}

use `Parametros_all', clear
foreach sub_sample in $Subsamples {
if "`sub_sample'" != "all" {
append using `Parametros_`sub_sample''
}
}

save matlab/Parametros, replace
export excel Wm Ww theta rho sigma group using matlab/Parametros, replace firstrow(variables)